import level1list from '@/json/level1list.json'
export function getLevel1List() {
    return JSON.parse(localStorage.getItem('level1list') || "[]");
}

export function getLevel2List(val) {
    const level1list = getLevel1List();
    const level1Obj = level1list.find(t => t.val === val) || {};
    return level1Obj.level2list || [];
}
export function getLevel2ListByName(val) {
    const level1list = getLevel1List();
    const level1Obj = level1list.find(t => t.label === val) || {};
    return level1Obj.level2list || [];
}

export function removeLevel2Item(level1Obj, item, index) {
    const level1list = getLevel1List();
    let level1 = level1list.find(t => t.val === level1Obj.val) || {};
    let level2list = level1.level2list;
    level2list.splice(index, 1);
    localStorage.setItem('level1list', JSON.stringify(level1list));
    transTaskList(index);
}

export function addLevel2List(level1Obj, obj) {
    const level1list = getLevel1List();
    let level1 = level1list.find(t => t.val === level1Obj.val) || {};
    if (level1.level2list) {
        level1.level2list.push(obj);
    } else {
        level1.level2list = [obj];
    }
    localStorage.setItem('level1list', JSON.stringify(level1list));
    transTaskList();
}

export function getTaskList() {
    const arr = JSON.parse(localStorage.getItem('tasklist') || "[]");
    return arr;
}

export function addTaskList(obj) {
    let tasklist = getTaskList();
    tasklist.push(obj);
    tasklist = tasklist.map((t) => {
        let level2list = concatList(t.level2list, getLevel2ListByName(t.level1));
        return {
            ...t,
            level2list,
        }
    })
    localStorage.setItem('tasklist', JSON.stringify(tasklist));
}

export function removeTaskItem(item, index) {
    const tasklist = getTaskList();
    tasklist.splice(index, 1);
    localStorage.setItem('tasklist', JSON.stringify(tasklist));
}

export function resetStorage() {
    const tasklist = getTaskList();
    tasklist.forEach((t) => {
        if (t.photo && t.photo.length) {
            t.photo.forEach((p) => {
                if (p.list && p.list.length) {
                    p.list.forEach((l) => {
                        localStorage.removeItem(l.item);
                    })
                }
            })
        }
        if (t.level2list && t.level2list.length) {
            t.level2list.forEach((l) => {
                if (l.photo && l.photo.length) {
                    l.photo.forEach((p) => {
                        if (p.list && p.list.length) {
                            p.list.forEach((s) => {
                                localStorage.removeItem(s.item);
                            })
                        }
                    })
                }
            })
        }
    });
    localStorage.setItem('level1list', JSON.stringify(level1list));
    localStorage.removeItem('tasklist');
}

function transTaskList(index) {
    let tasklist = getTaskList();
    tasklist = tasklist.map((t) => {
        let level2list = concatList(t.level2list, getLevel2ListByName(t.level1));
        if (index !== undefined) {
            level2list.splice(index, 1);
        }
        return {
            ...t,
            level2list,
        };
    })
    localStorage.setItem('tasklist', JSON.stringify(tasklist));
}
function concatList(arr1, arr2) {
    let arr = arr1 || [];
    arr2.forEach((t, i) => {
        if (!arr.filter((b, index) => { return index >= i }).some(a => a.label === t.label)) {
            arr.push(t);
        }
    });
    return arr;
}

// 参数一级分类label、图片list
export function saveLevel1Photo(level1, photo) {
    let tasklist = getTaskList();
    const index = tasklist.findIndex(t => t.level1 === level1);
    const photo1 = photo.map((t) => {
        let obj = { label: t.label };
        if (t.list && t.list.length) {
            obj.list = t.list.map((p, i1) => {
                const item = level1 + '^' + t.label + '^' + i1;
                localStorage.setItem(item, p.src);
                return { item };
            })
        }
        return obj;
    });
    tasklist[index].photo = photo1;
    localStorage.setItem('tasklist', JSON.stringify(tasklist));
}
// 参数一级分类label，二级分类index, 图片list
export function saveLevel2Photo(level1, level2Index, photo) {
    let tasklist = getTaskList();
    const index = tasklist.findIndex(t => t.level1 === level1);
    const photo1 = photo.map((t) => {
        let obj = { label: t.label };
        if (t.list && t.list.length) {
            obj.list = t.list.map((p, i1) => {
                const item = level1 + '^' + level2Index + '^' + t.label + '^' + i1;
                localStorage.setItem(item, p.src);
                return { item };
            })
        }
        return obj;
    });
    tasklist[index].level2list[level2Index].photo = photo1;
    localStorage.setItem('tasklist', JSON.stringify(tasklist));
}
